package 两个人送礼物线程池版;

public class MyRunnable implements Runnable{
    int num=100;
    @Override
    // 重写run()方法
    public void run() {
        // 无限循环
        while (true){
            // 如果发送礼物成功，则跳出循环
            if (sendGift()){
                break;
            }
        }
    }

    private synchronized boolean sendGift(){
        if (num>10){
            System.out.println(Thread.currentThread().getName()+"正在抽奖");
            num--;
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            System.out.println(Thread.currentThread().getName()+"抽奖成功，剩余"+num+"次");
            return false;
        }else {
            System.out.println(Thread.currentThread().getName()+"抽奖失败");
            return true;
        }
    }
}
